package com.hdax.controller;

import com.hdax.entity.Daily;
import com.hdax.entity.Employee;
import com.hdax.entity.Plan;
import com.hdax.service.DailyService;
import com.hdax.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/daily")
public class DailyController {

    @Autowired
    private PlanService planService;

    @Autowired
    private DailyService dailyService;

    /**
     * 展示页面
     * @return
     */
    @GetMapping(path = "/show")
    public String show() {
        return "daily/ShowProfiles";
    }

    @PostMapping(path = "/list")
    @ResponseBody
    @Transactional(rollbackFor=Exception.class)
    public Map<String,Object> list(int page, int limit,int id){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        List<Daily> dailies =new ArrayList<>();
        int count=0;
        if (page-1!=0 || limit!=0){
            dailies= dailyService.findAll((page-1)*limit,limit,id);
            count=dailyService.count();
        }else{
            dailies= dailyService.findAll((page-1)*limit,limit,id);
            for (int i = 0; i < dailies.size(); i++) {
                count++;
            }
        }

        //查询总条数(该名称下)
        map.put("count", count);
        map.put("data", dailies);
        return map;
    }


    /**
     * 添加工作日报
     * @param model
     * @param session
     * @return
     */
    @GetMapping(path = "/index")
    public String index(Model model, HttpSession session) {
        //获取当前登陆人的编号
        Employee employeeInfo=(Employee)session.getAttribute("loginUser");
        //登陆人id
        int id=employeeInfo.getEmpId();
        //根据id查询该id下的所有已接取的计划
        List<Plan> planList=planService.finByid(id);
        List<Plan> plans=new ArrayList<>();
        for (int i = 0; i < planList.size(); i++) {
            //判断是否接取计划
            int pstate=planList.get(i).getStatename().getId();
            if (pstate==10){
                Plan p=planService.selectByid(planList.get(i).getId());
                plans.add(p);
            }
        }
        model.addAttribute("plans",plans);
        return "daily/DaliyInsert";
    }

    /**
     * 添加
     * @param daily
     * @return
     */
    @PostMapping(path = "/insert")
    @ResponseBody
    @Transactional(rollbackFor=Exception.class)
    public Map<String,Object> insert(Daily daily,HttpSession session){
        Map<String,Object> map=new HashMap<String, Object>();
        Employee employeeInfo=(Employee)session.getAttribute("loginUser");
        int id=employeeInfo.getEmpId();
        map.put("code", 0);
        daily.setEmpid(id);
        if (daily.getSchedule()>=100){
            daily.setSchedule(100.0);
        }
        int resule= dailyService.insert(daily);
        if (resule>0){
            if (daily.getSchedule()==100 ||daily.getSchedule().equals("100") ||daily.getSchedule()>100){
                Plan plan=new Plan();
                plan.setId(daily.getPid());
                plan.setState(8);
                planService.updateBystate(plan);
            }
            map.put("msg","提交成功");
            map.put("data","success");
        }else {
            map.put("msg","提交失败");
            map.put("data","fail");
        }
        return map;
    }
}
